// ActionScript file
package com.spotme
{
	import flash.display.Shape;
	import flash.text.AntiAliasType;
	import flash.text.CSMSettings;
	import flash.text.FontStyle;
	import flash.text.TextColorType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextRenderer;
	
	import mx.core.UIComponent;
	
	public class UIShape extends UIComponent
	{
		private var _donated_percent:Number = 0.00;
		private var w:uint = 0;
		private var h:uint = 0;
		private var off_x:uint = 300;
		private var off_y:uint = 10;
		private var ctf:TextField;
		public function UIShape(width:uint = 460,height:uint = 600,max:uint = 10000,interval:uint = 1000)
		{
			var s:int = 0;
			var txtFormat:TextFormat = new TextFormat();
			txtFormat.size = 42;
			txtFormat.font = "Arial";
			for(var i:int = max; i > 0; i -= interval)
			{
				var tf:TextField = new TextField();
				tf.autoSize = TextFieldAutoSize.RIGHT;
				tf.text = i.toString();
				//trace(tf.width);
				tf.x = off_x - tf.width - 5;
				tf.y = off_y + s - 10;
				tf.textColor = 0x723d14;
				tf.antiAliasType = AntiAliasType.ADVANCED;
				s += 60;
				addChild(tf);
				tf.setTextFormat(txtFormat);
				TextRenderer.setAdvancedAntiAliasingTable("Arial", FontStyle.REGULAR,
					TextColorType.DARK_COLOR, new Array(new CSMSettings(42,0.8, -0.8)));
			}
			// counter
			this.ctf = new TextField();
			this.ctf.autoSize = TextFieldAutoSize.CENTER;
			this.ctf.text = (this._donated_percent * 100).toString();
			this.ctf.x = 500;
			this.ctf.y = 225;
			this.ctf.textColor = 0xFFFFFF;
			this.ctf.name = "Counter";
			
			this.h = height;
			this.w = width;
			this._donated_percent = 0;
			//trace(this.width,this.height,_donated_percent);
			Draw();
			super();
		}
		
		public function set donated_percent(donated_percent:Number):void
		{
			this._donated_percent = donated_percent;
			this.removeChild(this.getChildByName('donated_blood'));
			this.removeChild(this.getChildByName('Counter'));
			Draw();
		}
		
		private function Draw():void
		{
			//trace(this.w,this.h,_donated_percent);
			var square:Shape = new Shape();
			square.name = 'donated_blood';
			square.graphics.beginFill(0xDECCAA/*0xf2af00*/, 1);
			square.graphics.drawRect(off_x, off_y, this.w, this.h);
			
			var blood_h:int = (_donated_percent/100) * this.h;
			square.graphics.beginFill(0x723d14, 1);
			square.graphics.drawRect(off_x, off_y+(this.h-blood_h), this.w, blood_h);
			
			var triangleHeight:uint = 0.05 * this.h;
			square.graphics.beginFill(0xFFFFFF, 1);
			square.graphics.moveTo(off_x, off_y+this.h);

			square.graphics.lineTo(off_x, (off_y+this.h)-triangleHeight);
			square.graphics.lineTo(off_x+triangleHeight,off_y+this.h);
			square.graphics.lineTo(off_x,off_y+this.h); 
			
			triangleHeight = .4 * this.h;
			square.graphics.moveTo(off_x+this.w, off_y+this.h);
			square.graphics.lineTo(off_x+this.w, (off_y+this.h)-triangleHeight);
			square.graphics.lineTo((off_x+this.w)-triangleHeight,off_y+this.h);
			square.graphics.lineTo(off_x+this.w,off_y+this.h);
			
			
			square.graphics.endFill();
			this.addChild(square);
			
			this.ctf.text = Math.round((this._donated_percent * 100)).toString();
			var counter:TextFormat = new TextFormat();
			counter.size = 64;
			counter.font = "Arial";
			this.ctf.setTextFormat(counter);
			this.addChild(this.ctf);
		}
		
	}
}